package org.apache.james.mime4j.field;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.james.mime4j.MimeException;

public abstract class Field
{
  public static final String BCC = "Bcc";
  public static final String CC = "Cc";
  public static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
  public static final String CONTENT_TYPE = "Content-Type";
  public static final String DATE = "Date";
  private static final String FIELD_NAME_PATTERN = "^([\\x21-\\x39\\x3b-\\x7e]+)[ \t]*:";
  public static final String FROM = "From";
  public static final String REPLY_TO = "Reply-To";
  public static final String RESENT_BCC = "Resent-Bcc";
  public static final String RESENT_CC = "Resent-Cc";
  public static final String RESENT_DATE = "Resent-Date";
  public static final String RESENT_FROM = "Resent-From";
  public static final String RESENT_SENDER = "Resent-Sender";
  public static final String RESENT_TO = "Resent-To";
  public static final String SENDER = "Sender";
  public static final String SUBJECT = "Subject";
  public static final String TO = "To";
  private static final Pattern fieldNamePattern = Pattern.compile("^([\\x21-\\x39\\x3b-\\x7e]+)[ \t]*:");
  private static final DefaultFieldParser parser = new DefaultFieldParser();
  private final String body;
  private final String name;
  private final String raw;

  protected Field(String paramString1, String paramString2, String paramString3)
  {
    this.name = paramString1;
    this.body = paramString2;
    this.raw = paramString3;
  }

  public static DefaultFieldParser getParser()
  {
    return parser;
  }

  public static Field parse(String paramString)
    throws MimeException
  {
    String str1 = paramString.replaceAll("\r|\n", "");
    Matcher localMatcher = fieldNamePattern.matcher(str1);
    if (!(localMatcher.find()))
      throw new MimeException("Invalid field in string");
    String str2 = localMatcher.group(1);
    String str3 = str1.substring(localMatcher.end());
    if ((str3.length() > 0) && (str3.charAt(0) == ' '))
      str3 = str3.substring(1);
    return parser.parse(str2, str3, paramString);
  }

  public String getBody()
  {
    return this.body;
  }

  public String getName()
  {
    return this.name;
  }

  public String getRaw()
  {
    return this.raw;
  }

  public boolean isContentType()
  {
    return "Content-Type".equalsIgnoreCase(this.name);
  }

  public boolean isFrom()
  {
    return "From".equalsIgnoreCase(this.name);
  }

  public boolean isSubject()
  {
    return "Subject".equalsIgnoreCase(this.name);
  }

  public boolean isTo()
  {
    return "To".equalsIgnoreCase(this.name);
  }

  public String toString()
  {
    return this.raw;
  }
}